<?php
namespace app\admin\controller;

use app\admin\library\AdminController;
use cubox\Cubox;
use app\admin\library\User;

class Setting extends AdminController{
    public function onIndex(){
        $this->power('admin.setting');
        $this->pageTitle = '系统设置';
        $this->pageIcon  = 'fa fa-cog';
        $loginConfig = $this->app->config('login');
        $systemConfig = Cubox::config('system');
        $uploadConfig = Cubox::config('upload');

        $this->set('system', $systemConfig->toArray());
        $this->set('login', $loginConfig->toArray());
        $this->set('upload', $uploadConfig->toArray());
        $this->set('version', Cubox::$version);


        //ini_get upload_max_filesize
        $this->set('loginDirectOption', $this->loginDirectOption());
        $this->set('defaultAppOption', $this->defaultAppList());

        return $this->display();
    }

    public function onSaveLogin(){
        $this->power('admin.setting.login');
        $data['verificationCode'] =  $this->post('verificationCode','no');
        $data['failNumber'] =  $this->post('failNumber',$this->check()->type('int'));
        $data['failTime'] =  $this->post('failTime',$this->check()->type('int'));
        $data['wallpaper'] =  $this->post('wallpaper');
        $data['loginDirect'] =  $this->post('loginDirect');

        $this->app->config('login',true)->setData($data);

        return true;
    }

    public function onSaveSystem(){
        $this->power('admin.setting.system');
        $data['admin_title'] =  $this->post('admin_title');
        $data['default_app'] =  $this->post('default_app');
        Cubox::config('system')->setData($data);

        return true;
    }

    public  function onSaveUpload(){
        $this->power('admin.setting.upload');
        $data['disabled'] = $this->post('disabled',$this->check()->type('bool')->default(false));
        $data['folder_type'] = $this->post('folder_type',$this->check()->type('string')->default('/YYYYMM/DD/'));
        $data['file_size'] = $this->post('file_size',$this->check()->type('int')->default(10));
        $data['file_size_unit'] = $this->post('file_size_unit',$this->check()->type('string')->default('MB'));
        $data['file_type'] =  $this->post('file_type');
        Cubox::config('upload')->setData($data);
        return true;
    }


    public function onGetLoginDirect(){
        return $this->loginDirectOption();
    }

 

    private function loginDirectOption(){

        $loginDirectOption  = [];
        $appInfoList = Cubox::getAppList();
        $appList = User::app();
        foreach($appList as $app){
            $appData =  $appInfoList[$app]??[];
            if ($appData && isset($appData['admin']) &&  isset($appData['admin']['controller'])){
                $controller = $appData['admin']['controller'];
                $action =   $appData['admin']['action']??'index';
                
                $loginDirectOption[]  = [
                    'title' => $appData['title'],
                    'value' => serialize([
                        'controller' => $controller,
                        'action' => $action,
                        'app' => $app
                    ]),
                ];

            }
        }



        $plugs = $this->plug('AdminLoginDirect');
        
        foreach($plugs as $app => $plug){
            
            try{
                $list = $plug->getData();
           
                foreach($list as $val){

                    if (!isset($val['app'])) $val['app'] =  $app;

                    $loginDirectOption[]  = [
                        'title' => $val['title'],
                        'value' => serialize($val),
                    ];
                }
            }catch(\Exception $e){
                
            }
            
        }

        return $loginDirectOption;
    }


    private function defaultAppList(){
        $plugs = $this->plug('DefaultApp');

        $defaultAppList = [];

        foreach($plugs as $app=> $plug) {
            $defaultAppList[$app] = $plug->title;
        }

        return $defaultAppList;
    }
    
}